﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;
using System.Windows.Input;
using System.Windows;
using ShredTheLog.Common.MVVM;
using System.Globalization;
using ShredTheLog.Api.DataContracts;
using ShredTheLog.Api.DataContracts.TextChat;
using ShredTheLog.Model;
using ShredTheLog.Views;
using ShredTheLog.Controls;
using ShredTheLog.Api;
using System.ComponentModel.Composition;

namespace ShredTheLog.ViewModels
{
    public sealed class PrivateChatRequestViewModel : ViewModelBase
    {
        [Import]
        private IShredServices ShredServices { get; set; }

        private ITextChat TextChat { get; set; }

        public PrivateChatRequestViewModel(ITextChat textChat, IUserInfo caller)
        {
            Contract.Requires(textChat != null);
            Contract.Requires(caller != null);

            TextChat = textChat;
            Caller = new UserProfileViewModel(caller);
        }

        public UserProfileViewModel Caller { get; private set; }

        public bool IsWindowClosed
        {
            get { return _isWindowClosed; }
            set { _isWindowClosed = value; OnPropertyChanged(() => IsWindowClosed); }
        }
        private bool _isWindowClosed;

        #region Commands

        private ICommand _acceptCommand;
        public ICommand AcceptCommand
        {
            get
            {
                if (_acceptCommand == null)
                {
                    _acceptCommand = new DelegateCommand(
                        () =>
                        {
                            TextChat.Update(new SysMessageCommand(Caller, RequestAnswer.Accept));
                            ShredServices.OpenTextChatRoom(TextChatHelper.FormatPrivateRoomName(Caller, TextChat.Credentials));
                            IsWindowClosed = true;
                        });
                }
                return _acceptCommand;
            }
        }

        private ICommand _declineCommand;
        public ICommand DeclineCommand
        {
            get
            {
                if (_declineCommand == null)
                {
                    _declineCommand = new DelegateCommand(
                        () => 
                        {
                            TextChat.Update(new SysMessageCommand(Caller, RequestAnswer.Decline));
                            IsWindowClosed = true;
                        });
                }
                return _declineCommand;
            }
        }

        private ICommand _closeCommand;
        public ICommand CloseCommand
        {
            get
            {
                if (_closeCommand == null)
                {
                    _closeCommand = new DelegateCommand(() => { IsWindowClosed = true; });
                }
                return _closeCommand;
            }
        }

        #endregion
    }
}
